﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Galaxian
{
    class Program
    {
        public const int Rows = Field.Height;
        public const int Cols = Field.Width;

        private static void Initialize(Engine engine)
        {
            int startRow = 2;
            Random enemyShooting = new Random();

            //add enemies
            // TTTTTTT EEEEEE L      EEEEEE RRRRR   IIIIIII K   KK
            //    T    E      L      E      R   RR     I    K KK  
            //    T    EEEEEE L      EEEEEE RRRRR      I    KK    
            //    T    E      L      E      R   RR     I    K KK  
            //    T    EEEEEE LLLLLL EEEEEE R    RR IIIIIII K   KK

            string[] form = new string[5];
            form[0] = "TTTTTTT EEEEEE L      EEEEEE RRRRR   IIIII K   KK";
            form[1] = "   T    E      L      E      R   RR    I   K KK  ";
            form[2] = "   T    EEEEEE L      EEEEEE RRRRR     I   KK    ";
            form[3] = "   T    E      L      E      R   RR    I   K KK  ";
            form[4] = "   T    EEEEEE LLLLLL EEEEEE R    RR IIIII K   KK";

            for (int i = 0; i < 5; i++)
            {
                int col = 1;
                for (int j = 0; j < 49; j++)
                {
                    char symbol = form[i][j];
                    if (symbol != ' ')
                    {
                        bool EnemyIsShooting = false;
                        if (enemyShooting.Next(0, 11) > 8)
                        {
                            EnemyIsShooting = true;
                        }
                        EnemyShip enemy = new EnemyShip(new Position(startRow + i, col), EnemyIsShooting, enemyShooting.Next(10000, 20000), 'X');
                        engine.AddObject(enemy);
                    }
                    col++;
                }
            }

            //for (int i = startCol; i <= endCol; i++)
            //{
            //    bool EnemyIsShooting = false;
            //    if (enemyShooting.Next(0, 11) > 8)
            //    {
            //        EnemyIsShooting = true;
            //    }
            //    EnemyShip enemy = new EnemyShip(new Position(startRow, i), EnemyIsShooting, enemyShooting.Next(10000, 20000));
            //    engine.AddObject(enemy);
            //}

            //for (int i = startCol + 1; i <= endCol - 1; i++)
            //{
            //    bool EnemyIsShooting = false;
            //    if (enemyShooting.Next(0, 11) > 8)
            //    {
            //        EnemyIsShooting = true;
            //    }
            //    EnemyShip enemy = new EnemyShip(new Position(startRow + 1, i), EnemyIsShooting, enemyShooting.Next(10000, 20000));
            //    engine.AddObject(enemy);
            //}

            //for (int i = startCol; i <= endCol; i++)
            //{
            //    bool EnemyIsShooting = false;
            //    if (enemyShooting.Next(0, 11) > 8)
            //    {
            //        EnemyIsShooting = true;
            //    }
            //    EnemyShip enemy = new EnemyShip(new Position(startRow + 2, i), EnemyIsShooting, enemyShooting.Next(10000, 20000));
            //    engine.AddObject(enemy);
            //}
            //end

            //add my ship
            Ship ship = new Ship(new Position(Rows - 1, Cols / 2));
            engine.AddObject(ship);
            //end
        }

        static void Main(string[] args)
        {
            IRenderer renderer = new ConsoleRenderer(Rows, Cols);
            IUserInterface keyboard = new KeyboardInterface();

            Engine gameEngine = new Engine(renderer, keyboard, 200);

            keyboard.OnLeftPressed += (sender, eventInfo) =>
            {
                gameEngine.MoveShipLeft();
            };

            keyboard.OnRightPressed += (sender, eventInfo) =>
            {
                gameEngine.MoveShipRight();
            };

            keyboard.OnActionPressed += (sender, eventInfo) =>
            {
                gameEngine.ShipFire();
            };
            //Random enemyFire = new Random();
            //if (enemyFire.Next(0, 11) > 4)
            //{
            //    gameEngine.EnemyFire();
            //}
            Initialize(gameEngine);

            gameEngine.Run();
        }
    }
}
